home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0428.dms / q0428.adf / rs2fbm.c < prev    next >
C/C++ Source or Header  |  1992-05-12  |  5KB  |  208 lines

  1. /********************************************************/
  2. /* rs2fbm.c -- V1.3                                     */
  3. /* Convert rayshade (3.0 or 4.0) output into fbm format */
  4. /* (for non URT version of rayshade                     */
  5. /* use -4 option for rayshade 4.0 format (PL 0..4)      */
  6. /* need not use it for RS 4.0 since patchlevel 5        */
  7. /********************************************************/
  8.  
  9. #include <stdio.h>
  10.  
  11. #define ILL_PAR 102
  12. #define NO_MEM 100
  13. #define NOT_INP 104
  14. #define NOT_OUT 105
  15.  
  16. typedef unsigned char UBYTE;
  17. typedef unsigned long ULONG;
  18. typedef unsigned short UWORD;
  19.  
  20. #ifdef LATTICE_50
  21. #define ANSI_C
  22. #endif
  23.  
  24. #ifdef ANSI_C
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. void WriteRGBplanes(FILE *, UWORD, UWORD);
  29. #else
  30. extern void *malloc();
  31. void WriteRGBplanes();
  32. #endif
  33.  
  34. UBYTE *BitMap[1024];
  35.  
  36. # define FBM_MAX_TITLE        80        /* For title and credits */
  37.  
  38. # define BLACK            0        /* For 8bit files */
  39. # define WHITE            255        /* For 8bit files */
  40. # define BYTE            256        /* For 8bit files */
  41.  
  42. # define BLANKS        "                                             "
  43.  
  44. # define FBM_MAGIC    "%bitmap"
  45.  
  46. /* FBM bitmap headers in files (null terminated 12 character ascii strings) */
  47. typedef struct fbm_filehdr_struct {
  48.     char    magic[8];        /* 8 bytes FBM_MAGIC number */
  49.     char    cols[8];        /* Width in pixels */
  50.     char    rows[8];        /* Height in pixels */
  51.     char    planes[8];        /* Depth (1 for B+W, 3 for RGB) */
  52.     char    bits[8];        /* Bits per pixel */
  53.     char    physbits[8];        /* Bits to store each pixel */
  54.     char    rowlen[12];        /* Length of a row in bytes */
  55.     char    plnlen[12];        /* Length of a plane in bytes */
  56.     char    clrlen[12];        /* Length of colormap in bytes */
  57.     char    aspect[12];        /* ratio of Y to X of one pixel */
  58.     char    title[FBM_MAX_TITLE];    /* Null terminated title */
  59.     char    credits[FBM_MAX_TITLE];    /* Null terminated credits */
  60. } FBMFILEHDR;
  61.  
  62. void WriteRGBplanes(fpo, yres, rowlen)
  63. UWORD yres;
  64. UWORD rowlen;
  65. FILE *fpo;
  66. {
  67.   register UWORD i,k;
  68.  
  69.   /* Write RED plane */
  70.   for (i=0; i<yres; i++) {
  71.     for (k=0; k<rowlen*3; k += 3) {
  72.       putc(*(BitMap[i] +(ULONG)(k)),fpo);
  73.     };
  74.   };
  75.   /* Write GREEN plane */
  76.   for (i=0; i<yres; i++) {
  77.     for (k=0; k<rowlen*3; k += 3) {
  78.       putc(*(BitMap[i] +(ULONG)(k+1)),fpo);
  79.     };
  80.   };
  81.   /* Write BLUE plane */
  82.   for (i=0; i<yres; i++) {
  83.     for (k=0; k<rowlen*3; k += 3) {
  84.       putc(*(BitMap[i] +(ULONG)(k+2)),fpo);
  85.     };
  86.   };
  87. }
  88.  
  89. void Usage(CmdName)
  90. char *CmdName;
  91. {
  92.   fprintf(stderr,"Usage: %s [-4] inputfile [outputfile]\n",CmdName);
  93.   fprintf(stderr,"converts rayshade or mtv input to FBM format\n");
  94. }
  95.  
  96. main(argc,argv)
  97. int argc;
  98. char *argv[];
  99. {
  100.   register short i,k;
  101.   char *argstr;
  102.   FILE *fpi, *fpo;
  103.   static char InName[128], OutName[128];
  104.   FBMFILEHDR *fbm_hdr;
  105.   UWORD xres, yres;
  106.   UWORD rowlen;
  107.   char rs3fmt = !0;
  108.  
  109.   k=0;
  110.   if (argc<2) {
  111.     Usage(argv[0]);
  112.     exit(2);
  113.   };
  114.   for (i=1; i<argc; i++) {
  115.     argstr = argv[i];
  116.     if (*argstr == '-') {
  117.       argstr++;
  118.       if (*argstr == 'h') { /* help wanted */
  119.         Usage(argv[0]);
  120.         exit(0);
  121.       }
  122.       if (*argstr == '4') { /* help wanted */
  123.         rs3fmt = 0;
  124.       }
  125.       else fprintf(stderr,"Unknown option %s ignored\n",argv[i]);
  126.     }
  127.     else {
  128.       k++;
  129.       if (k==1) strcpy(InName,argstr);
  130.       else if (k==2) strcpy(OutName,argstr);
  131.       else fprintf(stderr,"Extraneous argument %s ignored\n",argstr);
  132.     }
  133.   };
  134.   /* Args are parsed, let's do our job */
  135.   if ((fpi=fopen(InName,"r")) == NULL) {
  136.     fprintf(stderr,"Cannot open file %s for input\n",InName);
  137.     exit(NOT_INP);
  138.   };
  139.  
  140.   fscanf(fpi,"%hd %hd",&xres,&yres);
  141.   if (yres > 1024) {
  142.     fclose(fpi);
  143.     fprintf(stderr,"Cannot handle images with more than 1024 rows\n");
  144.     exit(20);
  145.   };
  146.   if (getc(fpi) != '\n') {
  147.     fclose(fpi);
  148.     fprintf(stderr,"A newline char should have been found after the header\n");
  149.     fprintf(stderr,"Image is probably not in RS - format\n");
  150.     exit(20);
  151.   };
  152.  
  153.   rowlen = 2 * ((xres * 8 + 15) / 16);
  154.   
  155.   for (i=0; i<yres; i++) {
  156.     if ((BitMap[i] = malloc((unsigned)rowlen*3)) == NULL) {
  157.       fprintf(stderr,"not enough memory for malloc()\n");
  158.       exit(101);
  159.     };
  160.     setmem(BitMap[i],(unsigned)rowlen*3,0);
  161.   };
  162.  
  163.   if (rs3fmt) {
  164.     for (i=0; i<yres; i++) {
  165.       fread(BitMap[i],1,(unsigned)xres*3,fpi);
  166.     };
  167.   }
  168.   else {
  169.     for (i=yres-1; i>=0; i--) {
  170.       fread(BitMap[i],1,(unsigned)xres*3,fpi);
  171.     };
  172.   };
  173.  
  174.   fclose(fpi);
  175.  
  176.   if (k==1) fpo = stdout;
  177.   else if ((fpo=fopen(OutName,"w")) == NULL) {
  178.     fprintf(stderr,"Cannot open file %s for output\n",OutName);
  179.     exit(NOT_OUT);
  180.   };
  181.  
  182.   if ((fbm_hdr=malloc(sizeof(FBMFILEHDR))) == NULL) {
  183.     fprintf(stderr,"Not enough memory for malloc()\n");
  184.     exit(NO_MEM);
  185.   };
  186.  
  187.   strncpy(fbm_hdr->magic,FBM_MAGIC,8);
  188.   sprintf(fbm_hdr->cols,"%7u",(unsigned)xres);
  189.   sprintf(fbm_hdr->rows,"%7u",(unsigned)yres);
  190.   sprintf(fbm_hdr->planes,"%7u",3);
  191.   sprintf(fbm_hdr->bits,"%7u",8);
  192.   sprintf(fbm_hdr->physbits,"%7u",8);
  193.  
  194.   sprintf(fbm_hdr->rowlen,"%11u",(unsigned)rowlen);
  195.   sprintf(fbm_hdr->plnlen,"%11lu",(ULONG)rowlen*(ULONG)yres);
  196.   sprintf(fbm_hdr->clrlen,"%11u",0);
  197.   sprintf(fbm_hdr->aspect,"%11.6f",1.0);
  198.   strncpy(fbm_hdr->title,"",80);
  199.   strncpy(fbm_hdr->credits,"",80);
  200.   fwrite(fbm_hdr,1,sizeof(FBMFILEHDR),fpo);
  201.  
  202.   fprintf(stderr,"Writing  %u * %u * 3  FBM-Bitmap\n",(unsigned)rowlen,(unsigned)yres);
  203.   WriteRGBplanes(fpo,yres,rowlen);
  204.   fclose(fpo);
  205.   free(fbm_hdr);
  206.   for (i=0; i<yres; i++) free(BitMap[i]);
  207. }
  208.